﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Website.Models;
using Business.DAO;
using Business.Models;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Collections;

namespace Website.Controllers
{ 
    public class LopHocController : Controller
    {
        LopHocDAO dao = new LopHocDAO();
        LopHocModel model = null;

        //
        // GET: /LopHoc/

        public ViewResult Index(int id)
        {
            model = new LopHocModel();
            model._LopHoc = dao.GetByNienHoc(id);
            model._TreeView = GetTreeView();
            return View(model);
        }

        //
        // GET: /LopHoc/Details/5

        public ViewResult Details(int id)
        {
            LopHoc_Detail_Model model_detail = new LopHoc_Detail_Model();
            model_detail._LopHoc = dao.Get(id);
            IEnumerable<HocSinh> hocSinh = new HocSinhDAO().GetByLopHoc(id);
            model_detail._HocSinh = hocSinh;
            return View(model_detail);
        }

        private TreeViewModel GetTreeView()
        {
            TreeViewModel treeView = new TreeViewModel();
            treeView._TruongHoc = new TruongHocDAO().GetAll();
            treeView._NienHoc = new NienHocDAO().GetAll();
            treeView._LopHoc = new LopHocDAO().GetAll();
            return treeView;
        }

        [HttpPost]
        public ActionResult Details(HttpPostedFileBase file)
        { 
            if (Request.Files.Count > 0)
            {
                IEnumerable<HocSinh> hocsinh = InitializeWorkbook(Request.Files[0]);
                return PartialView("PreviewHocSinh", hocsinh);
            }
            else
            {
                return PartialView("Create", new LopHoc());
            }
        }

        [HttpPost]
        public ActionResult ImportHocSinh(HttpPostedFileBase file)
        {
            if (Request.Files.Count > 0)
            {
                IEnumerable<HocSinh> hocsinh = InitializeWorkbook(Request.Files[0]);
                return PartialView("PreviewHocSinh", hocsinh);
            }
            else
            {
                return PartialView("PreviewHocSinh", null);
            }
        }

        private IEnumerable<HocSinh> InitializeWorkbook(HttpPostedFileBase f)
        {
            //read the template via FileStream, it is suggested to use FileAccess.Read to prevent file lock.
            //book1.xls is an Excel-2007-generated file, so some new unknown BIFF records are added. 
            f.SaveAs(Server.MapPath("/" + f.FileName));
            using (FileStream file = System.IO.File.OpenRead(Server.MapPath("/" + f.FileName)))
            {
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
                return ConvertToDataTable(hssfworkbook);
            }

        }

        private IEnumerable<HocSinh> ConvertToDataTable(HSSFWorkbook hssfworkbook)
        {
            Sheet sheet = hssfworkbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

            ArrayList listHocSinh = new ArrayList();

            int currentRow = 0;

            HocSinh hocSinh = null;

            while (rows.MoveNext())
            {
                //neu la dong tieu de thi bo qua
                if (currentRow == 0) { currentRow++; continue; }

                Row row = (HSSFRow)rows.Current;

                hocSinh = new HocSinh();

                hocSinh.TenHocSinh = row.GetCell(0).StringCellValue;
                hocSinh.SoDienThoai = row.GetCell(1).StringCellValue;

                listHocSinh.Add(hocSinh);

                currentRow++;
            }

            return listHocSinh.Cast<HocSinh>().Select(h => h);
        }
    }
}